<?php
declare(strict_types=1);

namespace SixShop\Message\Entity;

use think\Model;

/**
 * 消息模板实体类
 */
class MessageTemplateEntity extends Model
{
    // 设置表名
    protected $name = 'message_templates';
    
    // 设置字段信息
    protected $schema = [
        'id'          => 'int',
        'code'        => 'string',
        'name'        => 'string',
        'title'       => 'string',
        'content'     => 'string',
        'type'        => 'int',
        'status'      => 'int',
        'create_time' => 'int',
        'update_time' => 'int',
    ];
    
    // 自动时间戳
    protected $autoWriteTimestamp = true;
    
    /**
     * 获取模板类型文本
     */
    public function getTypeTextAttr()
    {
        $types = [
            1 => '系统通知',
            2 => '活动通知',
            3 => '订单通知',
            4 => '物流通知',
            5 => '私信',
            6 => '其他',
        ];
        
        return $types[$this->type] ?? '未知类型';
    }
    
    /**
     * 获取状态文本
     */
    public function getStatusTextAttr()
    {
        $statuses = [
            0 => '禁用',
            1 => '启用',
        ];
        
        return $statuses[$this->status] ?? '未知状态';
    }
    
    /**
     * 解析模板内容
     *
     * @param array $data 替换数据
     * @return array 解析后的标题和内容
     */
    public function parse(array $data)
    {
        $title = $this->title;
        $content = $this->content;
        
        // 替换变量
        foreach ($data as $key => $value) {
            $title = str_replace('{' . $key . '}', $value, $title);
            $content = str_replace('{' . $key . '}', $value, $content);
        }
        
        return [
            'title' => $title,
            'content' => $content,
        ];
    }
}
